﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace BaseMethods
{
    public static class ShuffleData
    {
        //public static Tuple<List<T>, List<T>> ShuffleRatio<T>(this List<T> list, double ratio) where T : ICloneable
        //{
        //    var rand = new Random();
        //    var listB = list.Select(t => (T)t.Clone()).ToList();
        //    var listA = new List<T>();

        //    var n = (int)Math.Floor(list.Count * ratio);
        //    while (n > listA.Count)
        //    {
        //        int k = rand.Next(0, listB.Count);
        //        listA.Add(listB[k]);
        //        listB.RemoveAt(k);
        //    }
        //    return new Tuple<List<T>, List<T>>(listA, listB);
        //}

        public static List<T>[] ShuffleRatio<T>(this List<T> list, int folds) where T : ICloneable
        {
            var rand = new Random();
            var listB = list.Select(t => (T)t.Clone()).ToList();
            listB.Shuffle();
            var queue = new Queue<T>(listB);

            var n = listB.Count / folds;
            var res = new List<T>[folds];


            for (int i = 0; i < folds; i++)
            {
                res[i] = new List<T>();
                for (int j = 0; j < n; j++)
                {
                    var item = queue.Dequeue();
                    res[i].Add(item);
                }
            }
            while (queue.Count > 0)
            {
                res[folds - 1].Add(queue.Dequeue());
            }
            return res;
        }

        public static void Shuffle<T>(this IList<T> list)
        {
            var rng = new Random();
            int n = list.Count;
            while (n > 1)
            {
                n--;
                int k = rng.Next(n + 1);
                T value = list[k];
                list[k] = list[n];
                list[n] = value;
            }
        }
    }
}
